package quq.missq.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.List;

import quq.missq.beans.School;

public class SchoolGeter {

	private SchoolDataHelper mHelper;
	private SQLiteDatabase db;

	public SchoolGeter(Context context) {
        mHelper = new SchoolDataHelper(context);
        mHelper.openDatabase();
		db = mHelper.getDatabase();
	}

	public ArrayList<School> getSchool() {
        ArrayList<School> list = new ArrayList<>();
		Cursor cursor;
		try {
			String sql = "select m_nUniversity_ID,m_nUniversity_Name,m_nUniversity_Pinyin," +
                        "m_nProvince_Name,m_nProvince_Id from t_universities limit 30;";
			cursor = db.rawQuery(sql, null);
			cursor.moveToFirst();
            School school;
			while (cursor.getCount()>0&&!cursor.isLast()) {
				int sid = cursor.getInt(cursor.getColumnIndex("m_nUniversity_ID"));
                String sname = cursor.getString(cursor.getColumnIndex("m_nUniversity_Name"));
                String sname_py = cursor.getString(cursor.getColumnIndex("m_nUniversity_Pinyin"));
                String pname = cursor.getString(cursor.getColumnIndex("m_nProvince_Name"));
                int pid = cursor.getInt(cursor.getColumnIndex("m_nProvince_Id"));
                school = new School();
				school.setM_nUniversity_ID(sid);
                school.setM_nUniversity_Name(sname);
                school.setM_nUniversity_Pinyin(sname_py);
                school.setM_nProvince_Id(pid);
                school.setM_nProvince_Name(pname);
				list.add(school);
				cursor.moveToNext();
			}
            cursor.close();
		} catch (Exception e) { 
			e.printStackTrace();
		} finally {
          //  cursor.close();
		}
		return list;
	}

    public ArrayList<School> getSchool(String condition) {
        String conditionName = "m_nUniversity_Pinyin";
        if(StringUtils.hasChinese(condition)){
            conditionName = "m_nUniversity_Name";
            condition = condition.toLowerCase();
        }
        ArrayList<School> list = new ArrayList<>();
        Cursor cursor;
        try {
            String sql = "select m_nUniversity_ID,m_nUniversity_Name,m_nUniversity_Pinyin,m_nProvince_Name,m_nProvince_Id " +
                    "from t_universities where "+conditionName+" like '%"+condition+"%';";
            cursor = db.rawQuery(sql, null);
            cursor.moveToFirst();
            School school;
            while (cursor.getCount()>0&&!cursor.isLast()) {
                int sid = cursor.getInt(cursor.getColumnIndex("m_nUniversity_ID"));
                String sname = cursor.getString(cursor.getColumnIndex("m_nUniversity_Name"));
                String sname_py = cursor.getString(cursor.getColumnIndex("m_nUniversity_Pinyin"));
                String pname = cursor.getString(cursor.getColumnIndex("m_nProvince_Name"));
                int pid = cursor.getInt(cursor.getColumnIndex("m_nProvince_Id"));
                school = new School();
                school.setM_nUniversity_ID(sid);
                school.setM_nUniversity_Name(sname);
                school.setM_nUniversity_Pinyin(sname_py);
                school.setM_nProvince_Id(pid);
                school.setM_nProvince_Name(pname);
                list.add(school);
                cursor.moveToNext();
            }
            cursor.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {

        }
        return list;
    }

	public void close() {
		mHelper.closeDatabase();
		db.close();
	}
	
	
    

}
